package com.wjc.basemodule.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewStub
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.core.util.forEach
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.wjc.basemodule.R
import com.wjc.basemodule.BR
import com.wjc.basemodule.widget.DialogManager


/**
 * Description:: 对话框通用类
 * JcChen on 2020.09.18.12:32 AM
 */
abstract class BaseDialogFragment<BD : ViewDataBinding> : AppCompatDialogFragment() {

    var defaultPage: DefaultPage? = null
    private var mActivityProvider: ViewModelProvider? = null
    private var mFragmentProvider: ViewModelProvider? = null
    private var progressBar: ProgressBar? = null
    private var noContentView: View? = null
    private var networkErrorOrNoDataView: View? = null
    lateinit var mActivity: AppCompatActivity
    protected lateinit var binding: BD

    protected abstract val dataBindingConfig: DataBindingConfig

    protected abstract fun initDialog()

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as AppCompatActivity
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initViewModel()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, dataBindingConfig.layout, container, false)
        binding.lifecycleOwner = this
        if (dataBindingConfig.stateViewModel != null) {
            binding.setVariable(BR.vm, dataBindingConfig.stateViewModel)
        }
        dataBindingConfig.bindingParams.forEach { index, param -> binding.setVariable(index, param) }
        val loadingView = binding.root.findViewById<View>(R.id.loading_progress)
        val defaultView = binding.root.findViewById<View>(R.id.vs_error_or_no_data_container)
        if (loadingView != null || defaultView != null) {
            defaultPage = DefaultPage(loadingView, if (defaultView is ViewStub) defaultView else null)
        }
        return binding.root
    }

    override fun onStart() {
        super.onStart()
        initDialog()
    }

    override fun show(fragmentManager: FragmentManager, tag: String?) {
        val transaction = fragmentManager.beginTransaction()
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        transaction.add(this, tag).addToBackStack(null)
        transaction.commitAllowingStateLoss()
    }


    open fun initViewModel() {
    }


    protected val dialogManager: DialogManager
        protected get() = baseActivity!!.dialogManager

    private val baseActivity: BaseActivity<*>
        get() = activity as BaseActivity<*>


    protected fun <T : ViewModel?> getFragmentViewModel(modelClass: Class<T>): T {
        if (mFragmentProvider == null) {
            mFragmentProvider = ViewModelProvider(this)
        }
        return mFragmentProvider!![modelClass]
    }

    protected fun <T : ViewModel?> getActivityViewModel(modelClass: Class<T>): T {
        if (mActivityProvider == null) {
            mActivityProvider = ViewModelProvider(mActivity!!)
        }
        return mActivityProvider!![modelClass]
    }

    override fun onDestroyView() {
        super.onDestroyView()
        progressBar = null
        binding.unbind()
        networkErrorOrNoDataView = null
        noContentView = null
        progressBar = null
    }
}